#pragma once
#include <list>
#include <vector>
#include <algorithm>

#include "IGameStateView.h"
#include "IRender.h"

/**
========================================
Manages all rendering at a high level
========================================
*/
class RenderManager
{
public:
    RenderManager() { m_renderer = NULL; };
    /** Initialize the render manager */
    int Init( const ViewSettings &settings );
    /** Update the gamestate views */
	void Update(float  dt);
    /** Close off */
	void Shutdown();

    /** Render the scene */
    void Render(float dt);

    /** Set the Backend-Renderer */
    void SetRenderer( IRender* r ) { m_renderer = r; };
    /** Get the Backend */
    IRender* GetRenderer() { return m_renderer; };

    /*----------------------------------------------------------------
        GameStateView Manager - Manage the views of gamestates
        activate(display) the correct view for the current active
        states.
    -----------------------------------------------------------------*/

    /** Add a state view to the list, sort by z order */
    void AddStateView( IGameStateView* g ) {
        m_stateList.push_back( g );  
    };

    /** Remove a state */
    void RemoveStateView ( IGameStateView* g ) {
        type_StateView::iterator it = m_stateList.begin();
        for (; it != m_stateList.end(); ++it )
        {
            if ( (*it) == g )
            {
                delete g;
                m_stateList.erase( it );
                return;
            }
        }
    };

    /** Remove all States */
    void RemoveAll() {
        type_StateView::iterator it = m_stateList.begin();
        for (; it != m_stateList.end(); ++it )
        {
            delete (*it);
        }
        m_stateList.clear();
    };

    /** De/activate a game state view */
    void SetStateViewStatus( int id , bool active ) {
        type_StateView::iterator it = m_stateList.begin();
        for (; it != m_stateList.end(); ++it )
        {
            if ( (*it)->GetID() == id )
            {
                (*it)->SetActive( active );
                return;
            }
        }
    };

    /** Destructor */
    virtual ~RenderManager() { if( m_renderer) {delete m_renderer;m_renderer=0;} };
protected:
    
    typedef std::list< IGameStateView* >  type_StateView;
    type_StateView      m_stateList;        // gamestate view list

    
    IRender*            m_renderer;         // Rendering Backend
};

